package com.newcoder.community.service;

import com.newcoder.community.dao.AlphaDao;

import com.newcoder.community.dao.DiscussPostMapper;
import com.newcoder.community.dao.UserMapper;
import com.newcoder.community.entity.DiscussPost;
import com.newcoder.community.entity.User;
import com.newcoder.community.util.CommunityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * @author edison
 * @version 2023
 * @date 2024/1/14 17:20
 */

@Service //开启组件扫描，将该类注册到Spring容器中
//@Scope("prototype") // 默认为单例模式，每次调用都会返回同一个实例
public class AlphaService {

    @Autowired //自动装配,service依赖dao
    private AlphaDao alphaDao;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DiscussPostMapper discussPostMapper;

    @Autowired
    private TransactionTemplate transactionTemplate;


    public AlphaService() {
        System.out.println("实例化AlphaService");
    }

    @PostConstruct // 在构造器之后调用
    public void init() {
        System.out.println("初始化AlphaService");
    }

    @PreDestroy // 在销毁之前调用
    public void destroy() {
        System.out.println("销毁AlphaService");
    }

    public String find() {
        return alphaDao.select();
    }


    //测试事务
    //REQUIRED:支持当前事务(外部事务),如果不存在则创建新事务
    //REQUIRES_NEW:创建一个新事务,并且暂停当前事务(外部事务)
    //NESTED:如果当前存在事务(外部事务),则嵌套在该事务中执行(独立的提交和回滚),否则就和REQUIRED一样
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED) //声明式事务，只有在事务中才能执行
    public Object save1(){
        //新增用户
        User user = new User();
        user.setUsername("alpha");
        user.setSalt(CommunityUtil.generateUUID().substring(0,5));
        user.setPassword(CommunityUtil.md5("123"+user.getSalt()));
        user.setEmail("123@qq.com");
        user.setHeaderUrl("http://image.nowcoder.com/head/99t.png");
        user.setCreateTime(new java.util.Date());
        userMapper.insertUser(user);

        //增加帖子
        DiscussPost discussPost = new DiscussPost();
        discussPost.setUserId(user.getId());
        discussPost.setTitle("Hello");
        discussPost.setContent("新人报道");
        discussPost.setCreateTime(new java.util.Date());
        discussPostMapper.insertDiscussPost(discussPost);

        //测试异常
        Integer.valueOf("abc");

        return "ok";
    }


    //测试事务(编程式事务)
    public Object save2() {
        //设置隔离级别
        transactionTemplate.setIsolationLevel(TransactionTemplate.ISOLATION_READ_COMMITTED);
        //设置传播机制
        transactionTemplate.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRED);
        return transactionTemplate.execute(status -> {
            //新增用户
            User user = new User();
            user.setUsername("alpha");
            user.setSalt(CommunityUtil.generateUUID().substring(0,5));
            user.setPassword(CommunityUtil.md5("123"+user.getSalt()));
            user.setEmail("123@qq.com");
            user.setHeaderUrl("http://image.nowcoder.com/head/99t.png");
            user.setCreateTime(new java.util.Date());
            userMapper.insertUser(user);

            //增加帖子
            DiscussPost discussPost = new DiscussPost();
            discussPost.setUserId(user.getId());
            discussPost.setTitle("Hello");
            discussPost.setContent("新人报道");
            discussPost.setCreateTime(new java.util.Date());
            discussPostMapper.insertDiscussPost(discussPost);

            //测试异常
            Integer.valueOf("abc");

            return "ok";
        });
    }



}